<?php
/*
*自动登录
*@$_COOKIE['UserName'](用户名);
*@$_COOKIE['Password'](密码)
*/
function checklogin(){
	//dump($_SESSION);
	if(empty($_SESSION[C('USER_AUTH_KEY')])){
		$user=$_COOKIE['UserName'];
		$pwd=$_COOKIE['Password'];
		//dump($_COOKIE);
		if(empty($user) || empty($pwd)){
			return 0; //未储存cookie信息 尚未登录用户
		}else{
			$res=M('users')->query('SELECT UserId,UserName,UserType,TrueName,IsApproved,IsJury,OrgName FROM `users` WHERE ( UserName="'.$user.'" AND Password="'.$pwd.'" ) LIMIT 1');
			if(empty($res)){
				return -1; //用户名密码错误
			}else{
				foreach($res[0] as $key=>$value){
					$_SESSION[C('USER_AUTH_KEY')]=$value;
					//echo '11111111111111111111';
				}
			}
		}
	}
}
/*
*修改密码(原始密码判断在方法里面,此处不做判断)
*@$uid(用户id)
*@$newpass(新密码)
*/
function edit_pass($uid,$newpass){
	$m = M('users')->where("UserId={$uid}")->setField("Password","{$newpass}");
}
/*
*编辑头像
*@$uid(用户id)
*@$pic_path(伪图片地址，需要和路径拼凑，防止转义符的干扰)
*/
function edit_avatar($uid,$pic_path){
	$Users = M("Users");
	$data['Avatar'] = $pic_path; // 保存上传的照片的路径
	$Users->where("UserId={$uid}")->save($data); // 写入用户数据到数据库
	echo "<script>window.parent.updataface('{$data['Avatar']}');</script>";//父窗口执行函数
}
/*
*修改基本信息
*@$uid(用户id)
*@$form_array(用户基本信息)
*/
function edit_base_info($uid,$form_array){
	$m = M('users')->where("UserId={$uid}")->setField($form_array); 
}
/*
*修改详细信息(学生)
*@$uid(用户id)
*@$form_array(用户详细信息)
*/
function edit_studentdetail_info($uid,$form_array){
	$m = M('student')->where("UserId={$uid}")->setField($form_array); 
}
/*
*修改详细信息(教官,老师)
*@$uid(用户id)
*@$form_array(用户详细信息)
*/
function edit_detail_info($uid,$form_array){
	$m = M('teacher')->where("UserId={$uid}")->setField($form_array); 
}
/*
*创建用户信息(教官,老师)
*
*@$form(用户详细信息)
*/
function create_user($form){
	return $User=M('Users')->data($form)->add();  
}
/*f
*添加竞赛项目
*
*@$form_array(竞赛项目数据)
*/
function add_business_problems($form_array){
	$info = M('businessproblems')->add($form_array);
}
/*
*添加学习资源
*@$form_array(学习资源数据)
*/
function add_study_resources($form_array){
	return $info=M('studyresources')->add($form_array);
}
/*
*添加博文
*@$form_array(添加数据);
*/
function add_blog($form_array){
	return $info=M('bloglog')->add($form_array);
}
/*
申请志愿者
@form_array(添加数据);
*/
function add_volunteer($form_array){
	return $info=M('volunteer')->add($form_array);
}
//上传图片缩放
function doresize($mu_w,$oldpath,$newpath,$name,$mu_h=""){
		$ext=strrchr($oldpath,'.');
		switch($ext){
			case ".jpg":
				$yuan_im=imagecreatefromjpeg($oldpath);
				break;
			case ".gif":
				$yuan_im=imagecreatefromgif($oldpath);
				break;
			case ".png":
				$yuan_im=imagecreatefrompng($oldpath);
				break;
			case ".jpeg":
				$yuan_im=imagecreatefrompng($oldpath);
				break;
			default:
				die("文件类型不支持");
		}
		$yuan_w=imagesx($yuan_im);
		$yuan_h=imagesy($yuan_im);
		if($mu_h==""){
			$mu_h=$yuan_h/$yuan_w*$mu_w;
		}
		$mu_im=imagecreatetruecolor($mu_w,$mu_h);
		imagecopyresized($mu_im,$yuan_im,0,0,0,0,$mu_w,$mu_h,$yuan_w,$yuan_h);
		switch($ext){
			case ".jpg":
				imagejpeg($mu_im,"./$newpath/$name");
				break;
			case ".gif":
				imagejpeg($mu_im,"./$newpath/$name");
				break;
			case ".png":
				imagejpeg($mu_im,"./$newpath/$name");
				break;
			case ".jpeg":
				imagejpeg($mu_im,"./$newpath/$name");
				break;
			default:
				die("文件类型不支持");
		}
		imagedestroy($mu_im);
		imagedestroy($yuan_im);
	}
/* 
 *发送站内信的函数
 *@ $type (信件的类型,0为系统发送,1为当前用户发送)
 *@ $toid (收信人的用户ID)
 *@ $title (信件的标题)
 *@ $content (信件的内容)
 *@ 返回值: 成功返回 ture ,失败返回 false 
 */
function tosendmail($type,$toid,$title,$content){
		switch($type){
			case 0:
				$sender = 0;
				$SenderName = '系统信件';
				break;
			case 1:
				$sender = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
				$SenderName = getname($sender);
				break;
			default:
				echo '<script>alert("系统错误,参数设定不正确");</script>';
				exit;
		}
		if(!$toid || !$title || !$content){
			echo '<script>alert("系统错误,参数收信人ID,标题,内容等必须给定");</script>';
			exit;
		}
		$receiver = getname($toid);
		$createdate = date('Y-m-d H:i:s',time());
		$res = M()->execute('
			insert into mail(Id,Title,Content,Sender,IsDeleted,CreateDate,SenderName,Receiver,Receiverid,IsRead,Version) 
			values ( NULL,"'.$title.'","'.$content.'",'.$sender.',"0","'.$createdate.'","'.$SenderName.'","'.$receiver.'",'.$toid.',"0","0")
		');
		if($res)
			return true;
		else
			return false;
	}
/* 
 *查询用户名
 */
function getname($uid){
		$name=M()->query('SELECT TrueName FROM users WHERE UserId ='.$uid);
		return $name[0]['TrueName'];
	}
/* 
 *正则匹配过滤html标签
 */
function pregtag($arg){
	$content = $arg;
	$res = preg_replace("/<(.*?)>/",'',$content);
	return $res;
}
/* 
 *根据用户ID获取主页链接地址
 */
function geturl($uid){
	$usertype=M()->query('SELECT UserType FROM users WHERE UserId ='.$uid);
	$type = $usertype[0]['UserType'];
	switch($type){
		case 1:
		return 'Student/Home/index/id/'.$uid;
		break;		
		case 2:
		return 'Student/Home/index/id/'.$uid;
		break;
		case 3:
		return 'Teacher/Home/index/id/'.$uid;
		break;
		case 4:
		return 'Officer/Home/index/id/'.$uid;
		break;
	}
}